<?php

namespace app\v1\model;

use think\Model;
use think\Db;

/**
 * Class Dynamic
 * @package app\v1\model
 * 模块说明：这个类是处理动态模块的数据处理
 */

class Dynamic extends Base
{
    //获取动态列表
    public function getDynamicList(){
        //$map=;
        $userInfo=getUserInfo();
        $map['r.effect']=1;
        $map['r.active']=1;
        $map['r.orgid']=$userInfo['orgId'];
        $map['r.shopid']=$userInfo['schoolId'];
        $recordList=Db::name('Record')->alias('r')->join('Record_action ra','r.id=ra.recordid','LEFT')->join('users u','r.appenduserid=u.id','LEFT')->where($map)
            ->limit($this->pageCount)->page($this->currentPage)->field('r.id as recordId,u.headimgurl as avatar,u.name,r.appendtime as recordtime,r.twitter,r.files')->distinct(true)->order('r.id desc')->select();

        foreach($recordList as $key=>$value){
            $recordList[$key]['files']=$value['files']?explode(',',$value['files']):array();
            $recordList[$key]['commentList']=$this->getCommentList($value['recordId']);
            $recordList[$key]['pointList']=$this->getGoodPoint($value['recordId']);
        }
        $data=array('current_page'=>$this->currentPage,'recordList'=>$recordList);
      renderData(0,'ok',$data);
    }

    //获取某条动态的评论(还有回复的操作)
    public function getCommentList($recordId){
        $map['recordid']=$recordId;
        $map['rc.effect']=1;
        $map['rc.active']=1;
        $commentList=Db::name('Record_comment')->alias('rc')->join('users u','rc.userid=u.id','LEFT')->join('users tu','rc._touserid=tu.id','LEFT')
            ->field('rc.id as commentId,rc.recordid as recordId,rc.content,u.name,rc.userid as userId,tu.name as toName,rc._touserid as toUserId')->where($map)->select();
       return $commentList;
    }

    //获取点赞列表是
    public function getGoodPoint($recordId){
        $map['recordid']=$recordId;
        $map['ra.effect']=1;
        $map['ra.active']=1;
        $map['ra.typeid']=1;
        $goodPointList=Db::name('Record_action')->alias('ra')->join('users u','ra.userid=u.id')->where($map)->field('u.name,u.id as userId')->select();
        return $goodPointList;
    }

    //评论和回复
    public function addComment($recordId,$toUserId,$content){
        $userInfo=getUserInfo();
        checkSubmit($userInfo['parentId'],'addComment');
        $this->checkRecordId($recordId,$userInfo['schoolId']);
        $data=array();
        $data['appenduserid']=$userInfo['parentId'];
        $data['appendtime']=time();
        $data['modifyuserid']=$userInfo['parentId'];
        $data['modifytime']=time();
        $data['effect']=1;
        $data['active']=1;
        $data['recordid']=$recordId;
        $data['content']=$content;
        $data['userid']=$userInfo['parentId'];
        $data['_touserid']=$toUserId;
        $data['visual']=1;
        try{
            $res=Db::name('Record_comment')->insertGetId($data);
           $commentId=(int)$res;
           $map['rc.id']=$commentId;
            $map['recordid']=$recordId;
            $map['rc.effect']=1;
            $map['rc.active']=1;
            $commentInfo=Db::name('Record_comment')->alias('rc')->join('users u','rc.userid=u.id','LEFT')->join('users tu','rc._touserid=tu.id','LEFT')
                ->field('rc.id as commentId,rc.recordid as recordId,rc.content,u.name,rc.userid as userId,tu.name as toName,rc._touserid as toUserId')->where($map)->find();
        }catch (\Exception $e){
            renderInfo(400008,'系统繁忙,稍后重试');
        }

        if(!empty($res)){
            renderData(0,'提交成功',$commentInfo);
        }else{
            renderInfo(400008,'系统繁忙,稍后重试');
        }


    }
    //提交点赞（还左继续提交点赞）
    public function setPoint($recordId,$type){
        $userInfo=getUserInfo();
        $this->checkRecordId($recordId,$userInfo['schoolId']);
        $isPonit=$this->isPoint($recordId,$userInfo['parentId']);
        $data=array();
        if($type==0){
            if($isPonit){
                renderInfo(400001,'该动态已经点赞,请勿重复提交');
            }
        $hasPoint=$this->hasPoint($recordId,$userInfo['parentId']);
        if(!$hasPoint) {
            $data['appenduserid'] = $userInfo['parentId'];
            $data['appendtime'] = time();
            $data['modifyuserid'] = $userInfo['parentId'];
            $data['modifytime'] = time();
            $data['effect'] = 1;
            $data['active'] = 1;
            $data['recordid'] = $recordId;
            $data['userid'] = $userInfo['parentId'];
            $data['typeid'] = 1;
            $res = Db::name('Record_action')->insert($data);
        }else{
            $map['userid']=$userInfo['parentId'];
            $map['recordid']=$recordId;
            $data['effect']=1;
            $data['active']=1;
            $res=Db::name('Record_action')->where($map)->update($data);
        }
        }else if($type==1){

            if(!$isPonit){
                renderInfo(400001,'该动态还未点赞,无法提交点赞');
            }
            $map['userid']=$userInfo['parentId'];
            $map['recordid']=$recordId;
            $data['effect']=0;
            $data['active']=0;
            $res=Db::name('Record_action')->where($map)->update($data);
        }
        if(!empty($res)){
            renderInfo(0,'提交成功');
        }else{
            renderInfo(400008,'系统繁忙,稍后重试');
        }

    }
    //判断是否已经点赞
    //判断动态的Id是否存在
    public function checkRecordId($recordId,$schoolId){
        $map['id']=$recordId;
        $map['effect']=1;
        $map['active']=1;
        $map['shopid']=$schoolId;
        $res=Db::name('Record')->where($map)->find();
        if(empty($res)){
            renderInfo(400001,'动态参数有误');
        }

    }
    //判断账户是否已经点过赞
    public function isPoint($recordId,$userId){
        $map['effect']=1;
        $map['active']=1;
        $map['recordid']=$recordId;
        $map['userid']=$userId;
        $res=Db::name('Record_action')->where($map)->find();
        if(!empty($res)){
            return true;
        }else{
            return false;
        }
    }

    //判断是否历史有点过赞

    public function hasPoint($recordId,$userId){
        $map['recordid']=$recordId;
        $map['userid']=$userId;
        $map['effect']=0;
        $map['active']=0;
        $res=Db::name('Record_action')->where($map)->find();
        if(!empty($res)){
            return true;
        }else{
            return false;
        }

    }

    //删除自己的评论

    public function delComment($commentId){
        $userInfo=getUserInfo();
        $this->checkCommentId($commentId,$userInfo['parentId']);
        $map['id']=$commentId;
        $map['effect']=1;
        $map['active']=1;
        $map['id']=$commentId;
        $map['userid']=$userInfo['parentId'];
        $rs=Db::name('Record_comment')->where($map)->delete();
        if(!empty($rs)){
            renderInfo(0,'删除成功');
        }else{
            renderInfo(400008,'系统繁忙,请稍后重试');
        }

    }

   // 判断评论是否存在
    public function checkCommentId($commentId,$userId){
        $map['id']=$commentId;
        $map['effect']=1;
        $map['active']=1;
        $map['userid']=$userId;
        $res=Db::name('Record_comment')->where($map)->find();
        if(empty($res)){
            renderInfo(400001,'评论参数有误');
        }
    }

}
